package com.tac.se2_project2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.zip.GZIPInputStream;

public class FileCompressor {
	public final String FILE_NAME = "CloudZip.gz";
	public final String FILE_LOCATION = "/data/data/" + FILE_NAME;
	
	/*
	 * Unpacks the return statement to check if a username exists
	 */
	public boolean DecompressUniqueUsername(){
		GZIPInputStream zipIn;
		try {
			FileInputStream in = new FileInputStream(FILE_LOCATION);
			zipIn = new GZIPInputStream(in);
		}
		catch (IOException e) {
			System.out.println("Could not get file " + FILE_NAME + ".");
			return true;
		}
		boolean exists = true;
		try {
			ObjectInputStream ois = new ObjectInputStream(zipIn);
			Object obj = ois.readObject();
			exists = ((Integer)obj >= 1);
		}
		catch (IOException e) {
			System.out.println("Could not decompress " + FILE_NAME + ".");
		}
		catch (ClassNotFoundException ex) {
			System.out.println("Error decompressing " + FILE_NAME + ".");
		}
		
		return exists;
	}
	
	/**
	 * Unpackages the contacts from the gzip file
	 
	public List<Contact> DecompressContacts() {
		GZIPInputStream zipIn;
		try {
			FileInputStream in = new FileInputStream(ZIP_LOCATION);
			zipIn = new GZIPInputStream(in);
		}
		catch (IOException e) {
			System.out.println("Could not get file " + ZIP_FILENAME + ".");
			return null;
		}
		List<Contact> contactList = null;
		try {
			ObjectInputStream ois = new ObjectInputStream(zipIn);
			Object obj = ois.readObject();
			contactList = (List<Contact>)obj;
		}
		catch (IOException e) {
			System.out.println("Could not decompress " + ZIP_FILENAME + ".");
		}
		catch (ClassNotFoundException ex) {
			System.out.println("Error decompressing " + ZIP_FILENAME + ".");
		}
		
		return contactList;
	}*/
	
	/*
	 * Compresses the contacts list into a gzip file
	 
	public boolean CompressContacts(List<Contact> contactsList) {
		GZIPOutputStream zipOut;
		try { 
			FileOutputStream out = new FileOutputStream(ZIP_LOCATION);
			zipOut = new GZIPOutputStream(out);
		}
		catch (IOException e) {
			System.out.println("Could not create " + ZIP_FILENAME + ".");
			return false;
		}
		
		try {
			ObjectOutputStream oos = new ObjectOutputStream(zipOut);
			oos.writeObject(contactsList);
		}
		catch (IOException e) {
			System.out.println("Couldn't compress " + ZIP_FILENAME + ".");
		}
		
		try {
			zipOut.close();
		}
		catch (IOException e) {
			// cough cough..
		}
		
		return true;
	}*/
}
